package com.szholly.pro.watergas_server.ps.android.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.szholly.pro.watergas_server.ps.android.common.DateTime;

public class ServiceUtils {

	public static List<String> createInsertSqlList(String tableName, List<Map<String, Object>> list) throws Exception {
		if (tableName == null || tableName.isEmpty()) {
			throw new IllegalArgumentException("表名不能为空");
		}
		if (list == null) {
			return new ArrayList<String>();
		}
		List<String> insertSqlList = new ArrayList<>();
		String singleInsertSql;
		String key;
		String value;
		for (Map<String, Object> bean : list) {
			Iterator<String> keys = bean.keySet().iterator();
			String keyStr = "";
			String valueStr = "";
			while (keys.hasNext()) {
				key = (String) keys.next();
				keyStr += key + ",";
				Object opt = bean.get(key);
				if (opt == null) {
					value = "";
				} else if (opt instanceof DateTime) {
					DateTime dateTime = (DateTime) opt;
					value = "to_date('" + dateTime.toString("yyyy-MM-dd HH:mm:ss") + "','yyyy-mm-dd hh24:mi:ss')";
				} else if (opt instanceof Number) {
					value = String.valueOf(opt);
				} else if (opt instanceof String) {
					String str = (String) opt;
					if (str.toUpperCase().contains("SYS_GUID()")) {
						value = str;
					} else {
						value = "'" + opt + "'";
					}
				} else {
					value = opt.toString();
				}
				valueStr += value + ",";
			}
			if (!keyStr.isEmpty()) {
				keyStr = keyStr.substring(0, keyStr.length() - 1);
				valueStr = valueStr.substring(0, valueStr.length() - 1);
			}
			singleInsertSql = " insert into " + tableName + " (" + keyStr + ")" + " values (" + valueStr + ");";
			insertSqlList.add(singleInsertSql);
		}
		return insertSqlList;
	}
}
